import { createRouter, createWebHistory } from 'vue-router';

const routes = [
  {
    path: '/',
    component: () => import('@/views/Welcome.vue'),
    meta: {
      title: '客户管理'
    } 
  },
  {
    path: '/login',
    component: () => import('@/views/customer/Login.vue'),
    meta: {
      title: '客户登录'
    }
  },
  {
    path: '/register',
    component: () => import('@/views/customer/Register.vue'),
    meta: {
      title: '注册新客户'
    }
  },
  {
    path: '/list',
    component: () => import('@/views/customer/List.vue'),
    meta: {
      title: '用户列表'
    },
    beforeEnter(to, from) {
      const token = sessionStorage.getItem('token');
      if( token ) {
        return true;
      }
      return '/login';
    }
  },
  {
    path: '/logout',
    component: () => import('@/views/Welcome.vue'),
    beforeEnter() {
      sessionStorage.removeItem('token');
      return '/';
    }
  },
  {
    path: '/detail/:id',
    name: 'detail',
    component: () => import('@/views/customer/Detail.vue'),
    meta: {
      title: '用户详情'
    }
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
});

router.afterEach( to => {
  document.title = to.meta.title;
});

export default router;
